{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "85d45472-d00a-4ee5-aade-1a2ded6d899f",
   "metadata": {},
   "source": [
    "# Lesson 2: Summarize an audio file"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4c989221-ee73-4747-a01b-1659b74ce83f",
   "metadata": {},
   "source": [
    "### Import all needed packages"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ef48f6e8",
   "metadata": {
    "height": 132
   },
   "outputs": [],
   "source": [
    "import os\n",
    "from IPython.display import Audio\n",
    "import boto3\n",
    "import uuid\n",
    "import time\n",
    "import json\n",
    "from jinja2 import Template"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a37a4aff-a4c1-414a-8245-acd2683a32fc",
   "metadata": {},
   "source": [
    "### Let's start with transcribing an audio file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "50c639ed",
   "metadata": {
    "height": 46
   },
   "outputs": [],
   "source": [
    "audio = Audio(filename=\"dialog.mp3\")\n",
    "display(audio)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fb4c3dc1",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": [
    "s3_client = boto3.client('s3', region_name='us-west-2')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cf239332-7549-402d-ba72-e9661822ee55",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": [
    "bucket_name = os.environ['BucketName']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2ae5fae0",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": [
    "file_name = 'dialog.mp3'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7349f98c",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": [
    "s3_client.upload_file(file_name, bucket_name, file_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "56281396",
   "metadata": {
    "height": 46
   },
   "outputs": [],
   "source": [
    "transcribe_client = boto3.client('transcribe', region_name='us-west-2')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eee70c2d",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": [
    "job_name = 'transcription-job-' + str(uuid.uuid4())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1abfd8f8",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": [
    "job_name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "65a03320",
   "metadata": {
    "height": 199
   },
   "outputs": [],
   "source": [
    "response = transcribe_client.start_transcription_job(\n",
    "    TranscriptionJobName=job_name,\n",
    "    Media={'MediaFileUri': f's3://{bucket_name}/{file_name}'},\n",
    "    MediaFormat='mp3',\n",
    "    LanguageCode='en-US',\n",
    "    OutputBucketName=bucket_name,\n",
    "    Settings={\n",
    "        'ShowSpeakerLabels': True,\n",
    "        'MaxSpeakerLabels': 2\n",
    "    }\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "100eeb56",
   "metadata": {
    "height": 131
   },
   "outputs": [],
   "source": [
    "while True:\n",
    "    status = transcribe_client.get_transcription_job(TranscriptionJobName=job_name)\n",
    "    if status['TranscriptionJob']['TranscriptionJobStatus'] in ['COMPLETED', 'FAILED']:\n",
    "        break\n",
    "    time.sleep(2)\n",
    "print(status['TranscriptionJob']['TranscriptionJobStatus'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "135723db",
   "metadata": {
    "height": 573
   },
   "outputs": [],
   "source": [
    "if status['TranscriptionJob']['TranscriptionJobStatus'] == 'COMPLETED':\n",
    "    \n",
    "    # Load the transcript from S3.\n",
    "    transcript_key = f\"{job_name}.json\"\n",
    "    transcript_obj = s3_client.get_object(Bucket=bucket_name, Key=transcript_key)\n",
    "    transcript_text = transcript_obj['Body'].read().decode('utf-8')\n",
    "    transcript_json = json.loads(transcript_text)\n",
    "    \n",
    "    output_text = \"\"\n",
    "    current_speaker = None\n",
    "    \n",
    "    items = transcript_json['results']['items']\n",
    "    \n",
    "    for item in items:\n",
    "        \n",
    "        speaker_label = item.get('speaker_label', None)\n",
    "        content = item['alternatives'][0]['content']\n",
    "        \n",
    "        # Start the line with the speaker label:\n",
    "        if speaker_label is not None and speaker_label != current_speaker:\n",
    "            current_speaker = speaker_label\n",
    "            output_text += f\"\\n{current_speaker}: \"\n",
    "            \n",
    "        # Add the speech content:\n",
    "        if item['type'] == 'punctuation':\n",
    "            output_text = output_text.rstrip()\n",
    "            \n",
    "        output_text += f\"{content} \"\n",
    "        \n",
    "    # Save the transcript to a text file\n",
    "    with open(f'{job_name}.txt', 'w') as f:\n",
    "        f.write(output_text)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1446efee-a839-418e-a3c8-dbacb5187ad7",
   "metadata": {},
   "source": [
    "### Now, let's use an LLM"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3f34369c",
   "metadata": {
    "height": 46
   },
   "outputs": [],
   "source": [
    "bedrock_runtime = boto3.client('bedrock-runtime', region_name='us-west-2')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d506e51e",
   "metadata": {
    "height": 46
   },
   "outputs": [],
   "source": [
    "with open(f'{job_name}.txt', \"r\") as file:\n",
    "    transcript = file.read()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6f1e2a60",
   "metadata": {
    "height": 488
   },
   "outputs": [],
   "source": [
    "%%writefile prompt_template.txt\n",
    "I need to summarize a conversation. The transcript of the \n",
    "conversation is between the <data> XML like tags.\n",
    "\n",
    "<data>\n",
    "{{transcript}}\n",
    "</data>\n",
    "\n",
    "The summary must contain a one word sentiment analysis, and \n",
    "a list of issues, problems or causes of friction\n",
    "during the conversation. The output must be provided in \n",
    "JSON format shown in the following example. \n",
    "\n",
    "Example output:\n",
    "{\n",
    "    \"sentiment\": <sentiment>,\n",
    "    \"issues\": [\n",
    "        {\n",
    "            \"topic\": <topic>,\n",
    "            \"summary\": <issue_summary>,\n",
    "        }\n",
    "    ]\n",
    "}\n",
    "\n",
    "Write the JSON output and nothing more.\n",
    "\n",
    "Here is the JSON output:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0dadb797",
   "metadata": {
    "height": 46
   },
   "outputs": [],
   "source": [
    "with open('prompt_template.txt', \"r\") as file:\n",
    "    template_string = file.read()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "676313e8",
   "metadata": {
    "height": 63
   },
   "outputs": [],
   "source": [
    "data = {\n",
    "    'transcript' : transcript\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5f411f8a",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": [
    "template = Template(template_string)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b16e21d3",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": [
    "prompt = template.render(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "68584b09",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": [
    "print(prompt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e22cd7aa",
   "metadata": {
    "height": 267
   },
   "outputs": [],
   "source": [
    "kwargs = {\n",
    "    \"modelId\": \"amazon.titan-text-express-v1\",\n",
    "    \"contentType\": \"application/json\",\n",
    "    \"accept\": \"*/*\",\n",
    "    \"body\": json.dumps(\n",
    "        {\n",
    "            \"inputText\": prompt,\n",
    "            \"textGenerationConfig\": {\n",
    "                \"maxTokenCount\": 512,\n",
    "                \"temperature\": 0,\n",
    "                \"topP\": 0.9\n",
    "            }\n",
    "        }\n",
    "    )\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "98fdf1c5",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": [
    "response = bedrock_runtime.invoke_model(**kwargs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7154963f",
   "metadata": {
    "height": 46
   },
   "outputs": [],
   "source": [
    "response_body = json.loads(response.get('body').read())\n",
    "generation = response_body['results'][0]['outputText']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5dd9dd53",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": [
    "print(generation)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
